import request from '../request'

// 文件夹树节点接口
export interface FolderTreeNode {
  id: number
  name: string
  parentId?: number
  fileCount: number
  hasChildren: boolean
  children?: FolderTreeNode[]
  createdAt: string
  updatedAt: string
}

// 创建文件夹请求
export interface CreateFolderRequest {
  name: string
  parentId?: number
}

// 移动文件夹请求
export interface MoveFolderRequest {
  targetParentId: number
}

// 重命名文件夹请求
export interface RenameFolderRequest {
  name: string
}

// 文件夹API
export const folderApi = {
  /**
   * 获取文件夹树
   * @param parentId 父文件夹ID，null表示根目录
   * @param depth 加载深度，默认3层
   */
  getFolderTree(parentId?: number | null, depth: number = 3) {
    return request.get<FolderTreeNode[]>('/folders/tree', {
      params: {
        parentId,
        depth
      }
    })
  },

  /**
   * 创建文件夹
   * @param data 创建文件夹请求数据
   */
  createFolder(data: CreateFolderRequest) {
    return request.post<FolderTreeNode>('/folders', data)
  },

  /**
   * 重命名文件夹
   * @param folderId 文件夹ID
   * @param data 重命名请求数据
   */
  renameFolder(folderId: number, data: RenameFolderRequest) {
    return request.put<FolderTreeNode>(`/folders/${folderId}/rename`, data)
  },

  /**
   * 移动文件夹
   * @param folderId 文件夹ID
   * @param data 移动请求数据
   */
  moveFolder(folderId: number, data: MoveFolderRequest) {
    return request.put<void>(`/folders/${folderId}/move`, data)
  },

  /**
   * 删除文件夹
   * @param folderId 文件夹ID
   */
  deleteFolder(folderId: number) {
    return request.delete<void>(`/folders/${folderId}`)
  },

  /**
   * 获取文件夹详情
   * @param folderId 文件夹ID
   */
  getFolderDetail(folderId: number) {
    return request.get<FolderTreeNode>(`/folders/${folderId}`)
  },

  /**
   * 获取文件夹路径
   * @param folderId 文件夹ID
   */
  getFolderPath(folderId: number) {
    return request.get<{ path: string; breadcrumbs: Array<{ id: number; name: string }> }>(`/folders/${folderId}/path`)
  }
}
